package com.health.autoresponse.servicer.impl;/**
 * @Author：yuanhongchao
 * @Package：com.health.autoresponse.servicer.impl
 * @Project：health-cloud
 * @name：AutoResponseServiceImpl
 * @Date：25/05/2024 12:27 AM
 */

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.autoresponse.domain.AutoResponse;
import com.health.autoresponse.mapper.AutoResponseMapper;
import com.health.autoresponse.servicer.AutoResponseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 *@ClassName AutoResponseServiceImpl
 *@Description 描述
 *@Author HC.Y
 *@Date 25/05/2024 12:27 AM
 */
@Service
public class AutoResponseServiceImpl extends ServiceImpl<AutoResponseMapper, AutoResponse> implements AutoResponseService {
    @Autowired
    private AutoResponseMapper autoResponseMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public void updateStatus(Integer status) {
        //获取用户信息
//        User user = getUser();
//        if (user==null){
//            throw new RuntimeException("请先登录");
//        }
        if (status==null || status<1 || status>2){
            throw new RuntimeException("请选择正确的状态");
        }
        //根据用户信息获取当前用户自动回复信息
        AutoResponse autoResponse = autoResponseMapper.selectByUserId(1);//根据用户id获取
        //判断当前状态是否与要修改的状态一致
        if (status==1 && autoResponse.getStatus()==1){
            throw new RuntimeException("自动回复已关闭，请勿重复关闭");
        }
        if (status==2 && autoResponse.getStatus()==2){
            throw new RuntimeException("自动回复已开启，请勿重复开启");
        }
        //根据用户信息和状态修改回复状态，如果修改状态为开启，回复内容默认选择第一个
        Integer update = autoResponseMapper.updateStatus(1,status);//根据用户id修改
        if (update<=0){
            throw new RuntimeException("状态修改失败");
        }
    }

    /**
     * 修改回复内容
     * @param contentId
     */
    @Override
    public void updateContentId(Integer contentId) {
        //获取用户信息
//        User user = getUser();
//        if (user==null){
//            throw new RuntimeException("请先登录");
//        }
        if (contentId==null || contentId<1){
            throw new RuntimeException("请选择回复内容");
        }
        //根据用户信息获取当前用户自动回复信息
        AutoResponse autoResponse = autoResponseMapper.selectByUserId(1);//根据用户id获取
        if (autoResponse==null || autoResponse.getStatus()!=2){
            throw new RuntimeException("自动回复未开启，请先开启");
        }
        //根据用户信息和回复内容id修改回复内容
        Integer update = autoResponseMapper.updateContentId(1,contentId);//根据用户id修改
        if (update<=0){
            throw new RuntimeException("回复内容修改失败");
        }
    }

    /**
     * 修改自定义回复内容
     * @param content
     */
    @Override
    public void updateCustomContent(String content) {
        //获取用户信息
//        User user = getUser();
//        if (user==null){
//            throw new RuntimeException("请先登录");
//        }
        if (content==null || content.length()<1){
            throw new RuntimeException("请输入回复内容");
        }
        //根据用户信息获取当前用户自动回复信息
        AutoResponse autoResponse = autoResponseMapper.selectByUserId(1);//根据用户id获取
        //自动回复id修改回复内容
        Integer update = autoResponseMapper.updateCustomContent(autoResponse.getResponseId(),content);
        if (update<=0){
            throw new RuntimeException("回复内容修改失败");
        }
    }

    /**
     * 获取当前用户信息
     * @return
     */
//    private User getUser() {
//        String token = request.getHeader();
//    }

}
